// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Summary description for AddCategoryDlg.
	/// </summary>
	public class ParentProductDlg : System.Windows.Forms.Form
	{
      private AuthoringEngine eng = null;
      protected XmlNode objProductNode = null;
      private System.Windows.Forms.Button btnOK;
      private System.Windows.Forms.Button btnCancel;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.Button btnNew;
      private System.Windows.Forms.TextBox txtProductId;
      private System.Windows.Forms.Label lblProductId;
      private System.Windows.Forms.Label lblCategoryName;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.Label lblProductName;
      private System.Windows.Forms.Panel panel1;
      private System.Windows.Forms.ComboBox ddlRuleFileName;
      private System.Windows.Forms.Button btnSearch;
      private System.Windows.Forms.CheckBox chkAllowDiscounting;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
      
      public AuthoringEngine Engine
      {
         get { return eng; }
         set { eng = value; }
      }
      
      public XmlNode ProductNode
      {
         get { return objProductNode; }
         set { objProductNode = value; }
      }

		public ParentProductDlg()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.btnOK = new System.Windows.Forms.Button();
         this.btnCancel = new System.Windows.Forms.Button();
         this.label1 = new System.Windows.Forms.Label();
         this.txtProductId = new System.Windows.Forms.TextBox();
         this.lblProductId = new System.Windows.Forms.Label();
         this.lblCategoryName = new System.Windows.Forms.Label();
         this.label4 = new System.Windows.Forms.Label();
         this.ddlRuleFileName = new System.Windows.Forms.ComboBox();
         this.btnNew = new System.Windows.Forms.Button();
         this.btnSearch = new System.Windows.Forms.Button();
         this.label2 = new System.Windows.Forms.Label();
         this.lblProductName = new System.Windows.Forms.Label();
         this.panel1 = new System.Windows.Forms.Panel();
         this.chkAllowDiscounting = new System.Windows.Forms.CheckBox();
         this.SuspendLayout();
         // 
         // btnOK
         // 
         this.btnOK.Location = new System.Drawing.Point(120, 232);
         this.btnOK.Name = "btnOK";
         this.btnOK.Size = new System.Drawing.Size(80, 23);
         this.btnOK.TabIndex = 11;
         this.btnOK.Text = "OK";
         this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
         // 
         // btnCancel
         // 
         this.btnCancel.Location = new System.Drawing.Point(208, 232);
         this.btnCancel.Name = "btnCancel";
         this.btnCancel.Size = new System.Drawing.Size(80, 23);
         this.btnCancel.TabIndex = 12;
         this.btnCancel.Text = "Cancel";
         this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(8, 16);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(88, 16);
         this.label1.TabIndex = 0;
         this.label1.Text = "Category Name:";
         // 
         // txtProductId
         // 
         this.txtProductId.Location = new System.Drawing.Point(8, 72);
         this.txtProductId.Name = "txtProductId";
         this.txtProductId.Size = new System.Drawing.Size(192, 21);
         this.txtProductId.TabIndex = 3;
         this.txtProductId.Text = "Untitled";
         // 
         // lblProductId
         // 
         this.lblProductId.Location = new System.Drawing.Point(8, 56);
         this.lblProductId.Name = "lblProductId";
         this.lblProductId.Size = new System.Drawing.Size(104, 16);
         this.lblProductId.TabIndex = 2;
         this.lblProductId.Text = "Product ID:";
         // 
         // lblCategoryName
         // 
         this.lblCategoryName.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.lblCategoryName.Location = new System.Drawing.Point(96, 16);
         this.lblCategoryName.Name = "lblCategoryName";
         this.lblCategoryName.Size = new System.Drawing.Size(168, 16);
         this.lblCategoryName.TabIndex = 1;
         this.lblCategoryName.Text = "Name of Category";
         // 
         // label4
         // 
         this.label4.Location = new System.Drawing.Point(8, 144);
         this.label4.Name = "label4";
         this.label4.Size = new System.Drawing.Size(104, 16);
         this.label4.TabIndex = 8;
         this.label4.Text = "Rule File Name:";
         // 
         // ddlRuleFileName
         // 
         this.ddlRuleFileName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
         this.ddlRuleFileName.Location = new System.Drawing.Point(8, 160);
         this.ddlRuleFileName.Name = "ddlRuleFileName";
         this.ddlRuleFileName.Size = new System.Drawing.Size(192, 21);
         this.ddlRuleFileName.Sorted = true;
         this.ddlRuleFileName.TabIndex = 9;
         // 
         // btnNew
         // 
         this.btnNew.Location = new System.Drawing.Point(208, 160);
         this.btnNew.Name = "btnNew";
         this.btnNew.Size = new System.Drawing.Size(80, 23);
         this.btnNew.TabIndex = 10;
         this.btnNew.Text = "New...";
         this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
         // 
         // btnSearch
         // 
         this.btnSearch.Location = new System.Drawing.Point(208, 72);
         this.btnSearch.Name = "btnSearch";
         this.btnSearch.Size = new System.Drawing.Size(80, 23);
         this.btnSearch.TabIndex = 4;
         this.btnSearch.Text = "Search...";
         this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(8, 104);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(88, 16);
         this.label2.TabIndex = 5;
         this.label2.Text = "Product Name:";
         // 
         // lblProductName
         // 
         this.lblProductName.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.lblProductName.Location = new System.Drawing.Point(88, 104);
         this.lblProductName.Name = "lblProductName";
         this.lblProductName.Size = new System.Drawing.Size(176, 16);
         this.lblProductName.TabIndex = 6;
         this.lblProductName.Text = "Name of Product";
         // 
         // panel1
         // 
         this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
         this.panel1.Location = new System.Drawing.Point(8, 128);
         this.panel1.Name = "panel1";
         this.panel1.Size = new System.Drawing.Size(280, 4);
         this.panel1.TabIndex = 7;
         // 
         // chkAllowDiscounting
         // 
         this.chkAllowDiscounting.Location = new System.Drawing.Point(8, 192);
         this.chkAllowDiscounting.Name = "chkAllowDiscounting";
         this.chkAllowDiscounting.Size = new System.Drawing.Size(176, 24);
         this.chkAllowDiscounting.TabIndex = 19;
         this.chkAllowDiscounting.Text = "Allow Discounting";
         // 
         // ParentProductDlg
         // 
         this.AcceptButton = this.btnOK;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
         this.ClientSize = new System.Drawing.Size(298, 264);
         this.Controls.Add(this.chkAllowDiscounting);
         this.Controls.Add(this.panel1);
         this.Controls.Add(this.lblProductName);
         this.Controls.Add(this.label2);
         this.Controls.Add(this.btnSearch);
         this.Controls.Add(this.btnNew);
         this.Controls.Add(this.ddlRuleFileName);
         this.Controls.Add(this.label4);
         this.Controls.Add(this.lblCategoryName);
         this.Controls.Add(this.lblProductId);
         this.Controls.Add(this.txtProductId);
         this.Controls.Add(this.label1);
         this.Controls.Add(this.btnCancel);
         this.Controls.Add(this.btnOK);
         this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "ParentProductDlg";
         this.ShowInTaskbar = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Parent Product";
         this.Load += new System.EventHandler(this.ParentProductDlg_Load);
         this.ResumeLayout(false);

      }
		#endregion

      private void ParentProductDlg_Load(object sender, System.EventArgs e)
      {
         txtProductId.Text = objProductNode.Attributes["productId"].Value;

         lblProductName.Text = "---";

         if (txtProductId.Text != "")
         {
            LoadProduct();
         }

         XmlNode  objCategoryNode = objProductNode.ParentNode;
         lblCategoryName.Text = objCategoryNode.Attributes["name"].Value;

         RefreshRuleFileNameList();

         // Allow Discounting
         bool bAllowDiscounting = true;

         if (objProductNode.Attributes["allowDiscounting"] != null)
         {
            bAllowDiscounting = Convert.ToBoolean(objProductNode.Attributes["allowDiscounting"].Value);
         }
         chkAllowDiscounting.Checked = bAllowDiscounting;
      }

      private void btnOK_Click(object sender, System.EventArgs e)
      {
         if (LoadProduct())
         {
            objProductNode.Attributes["productId"].Value = txtProductId.Text;

            // Rule file name
            if (objProductNode.Attributes["ruleFileName"] == null)
            {
               XmlAttribute attrib = objProductNode.OwnerDocument.CreateAttribute("", "ruleFileName", "");
               objProductNode.Attributes.Append(attrib);
            }

            objProductNode.Attributes["ruleFileName"].Value = ddlRuleFileName.Text;
            
            if (objProductNode.Attributes["ruleFileName"].Value == "[None]")
               objProductNode.Attributes["ruleFileName"].Value = "";

            // Allow discounting
            if (objProductNode.Attributes["allowDiscounting"] == null)
            {
               XmlAttribute attrib = objProductNode.OwnerDocument.CreateAttribute("", "allowDiscounting", "");
               objProductNode.Attributes.Append(attrib);
            }

            objProductNode.Attributes["allowDiscounting"].Value = chkAllowDiscounting.Checked.ToString();
               
            this.DialogResult = DialogResult.OK;
            this.Close();
         }
      }

      private void btnCancel_Click(object sender, System.EventArgs e)
      {
         this.Close();      
      }

      private bool LoadProduct()
      {
         bool  bResult = false;
         
         Cursor oldCursor = Cursor.Current;
         Cursor.Current = Cursors.WaitCursor; 
         Product objProduct = eng.LoadProductFromProductNumber(txtProductId.Text);
         Cursor.Current = oldCursor;

         if (objProduct == null)
         {
            lblProductName.Text = "---";
            MessageBox.Show(this, "Product ID " + txtProductId.Text + " not found.", "Load Product");
         }
         else
         {
            lblProductName.Text = objProduct.Name;
            bResult = true;
         }

         return bResult;
      }
         

      private void RefreshRuleFileNameList()
      {
         ddlRuleFileName.Items.Clear();

         ddlRuleFileName.Items.Add("[None]");

         XmlNodeList objItemList = objProductNode.OwnerDocument.SelectNodes("//Item");

         foreach (XmlNode node in objItemList)
         {
            XmlAttribute   attrib = node.Attributes["ruleFileName"];

            if (attrib != null)
            {                
               if (-1 == ddlRuleFileName.FindString(attrib.Value))
                  ddlRuleFileName.Items.Add(attrib.Value);
            }
         }

         string strRuleFileName = "";

         if (objProductNode.Attributes["ruleFileName"] != null)
            strRuleFileName = objProductNode.Attributes["ruleFileName"].Value;
 
         int nPos = ddlRuleFileName.FindString(strRuleFileName);

         if (nPos == -1)
         {
            nPos = ddlRuleFileName.FindString("[None]");
         }

         ddlRuleFileName.SelectedIndex = nPos;
      }

      private void btnNew_Click(object sender, System.EventArgs e)
      {
         FileNameDlg dlg = new FileNameDlg();

         dlg.ShowDialog(this);

         if (dlg.DialogResult == DialogResult.OK)
         {
            int nPos = ddlRuleFileName.Items.Add(dlg.FileName);
            ddlRuleFileName.SelectedIndex = nPos;
         }
      }

      private void btnSearch_Click(object sender, System.EventArgs e)
      {
         LoadCrmProductDlg dlg = new LoadCrmProductDlg();

         dlg.Engine = this.Engine;

         if (DialogResult.OK == dlg.ShowDialog(this))
         {
            txtProductId.Text = dlg.SelectedProduct.ProductNumber;
            lblProductName.Text = dlg.SelectedProduct.Name;
         }
      }


	} // end class
}
